package com.getpebble.android.util;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.framework.util.SideloadingUtils;
import com.getpebble.android.main.fragment.FragmentMetaData.FragmentType;

public class Sideloading {
    private static final String TAG = Sideloading.class.getSimpleName();
    private String mAction;
    private String mExtension;
    private String mFilePath;
    private Bundle mFragmentArgs;
    private FragmentType mFragmentType;
    private Intent mIntent;
    private String mScheme;
    private Uri mUri;
    private boolean mValid = false;

    public Sideloading(Context context, Intent intent) {
        this.mIntent = intent;
        if (this.mIntent != null) {
            this.mAction = this.mIntent.getAction();
            if (this.mAction != null && this.mAction.equals("android.intent.action.VIEW")) {
                this.mUri = this.mIntent.getData();
                if (this.mUri != null) {
                    this.mScheme = this.mUri.getScheme();
                    if (this.mScheme != null) {
                        Trace.debug(TAG, "uri = " + this.mUri + " scheme = " + this.mScheme);
                        Trace.debug(TAG, "type = " + intent.getType() + " dataString = " + intent.getDataString());
                        if (SideloadingUtils.isContentScheme(this.mUri)) {
                            this.mFilePath = SideloadingUtils.getContentName(context.getContentResolver(), this.mUri);
                        } else if (this.mScheme.equals("http") || this.mScheme.equals("https") || this.mScheme.equals("file")) {
                            this.mFilePath = this.mUri.getPath();
                        }
                        if (this.mFilePath == null) {
                            return;
                        }
                        if (this.mFilePath.endsWith(".pbz")) {
                            this.mExtension = ".pbz";
                            this.mValid = true;
                        } else if (this.mFilePath.endsWith(".pbw")) {
                            this.mExtension = ".pbw";
                            this.mValid = true;
                        } else if (this.mFilePath.endsWith(".pbl")) {
                            this.mExtension = ".pbl";
                            this.mValid = true;
                        }
                    }
                }
            }
        }
    }

    public boolean isValidSideloadingIntent() {
        return this.mValid;
    }

    public FragmentType getFragmentTypeToSwitch() {
        if (this.mFragmentType == null) {
            loadFragmentData();
        }
        return this.mFragmentType;
    }

    public Bundle getFragmentArgs() {
        if (this.mFragmentArgs == null) {
            loadFragmentData();
        }
        return this.mFragmentArgs;
    }

    private void loadFragmentData() {
        if (isValidSideloadingIntent()) {
            this.mFragmentArgs = new Bundle();
            this.mFragmentArgs.putParcelable("extra_sideloading_uri", this.mUri);
            if (this.mExtension.equals(".pbz")) {
                this.mFragmentArgs.putInt("extra_sideloading_type", 1);
            } else if (this.mExtension.equals(".pbw")) {
                this.mFragmentArgs.putInt("extra_sideloading_type", 2);
            } else if (this.mExtension.equals(".pbl")) {
                this.mFragmentArgs.putInt("extra_sideloading_type", 3);
            }
            this.mFragmentType = FragmentType.MY_PEBBLE;
        }
    }
}
